# -*- coding : utf_8 -*-
"""
=======================================
Time : 2025/3/22 22:25
Author : 田霄汉
Email : 522989570@qq.com
File : requests_util.py
Project : pytest_demo
=======================================
"""
import requests
from utils.debugtalk_util import DebugtalkUtil
from builtins import str
from debug_talk import DebugTalk
from utils.log_util import logs


class RequestsUtil:

    sess = requests.session()

    @staticmethod
    def send_request(method, url, **kwargs):
        """
        统一请求封装
        :param method: 请求方法
        :param url: 请求url
        :param kwargs: 其他请求参数
        :return: 响应结果res
        """

        method = str(method).lower()  # 转换小写
        # # 基础路径的拼接和替换
        # url = self.base_url + self.replace_value(url)
        # 参数替换
        try:
            for key, value in kwargs.items():
                if key in ['params', 'data', 'json', 'headers']:
                    # kwargs[key] = self.replace_value(value)
                    # 热加载技术，热加载DebugTalk模块中的函数
                    kwargs[key] = DebugtalkUtil(DebugTalk()).debugtalk_replace_value(value)
                elif key == "files":
                    for file_key, file_path in value.items():
                        value[file_key] = open(file_path, 'rb')
            res = RequestsUtil.sess.request(method, url, **kwargs)
            logs.info(f'接口请求成功')
            return res
        except Exception as e:
            logs.error(f'接口请求错误，请检查错误信息{e}')
